#include "Login.h"
#include "../Game.h"

Login::Login()
{
	m_loginGameServerId = 0;
	//ctor
}

Login::~Login()
{
	//dtor
}

void Login::OnL2WLoginInfo(Message * _pMsg)
{


}

void Login::Init()
{
	m_WorldServerInfo.Init();
}

bool Login::UpdateLoginServer()
{
	//Debug::Log("==>> UpdateLoginServer()");
	GameServerInfo* pLoginGameServerInfo = m_WorldServerInfo.GetLoginGameServer();
	if(pLoginGameServerInfo == nullptr)
	{
		mp_LoginGameServer = nullptr;
		m_WorldServerInfo.m_State = eServerState::Refuse;
		m_loginGameServerId = 0;
		return true;
	}

	if(pLoginGameServerInfo->m_id == m_loginGameServerId)
	{
		return false;
	}

	mp_LoginGameServer = pLoginGameServerInfo;
	m_WorldServerInfo.m_State = eServerState::Fine;
	m_loginGameServerId = pLoginGameServerInfo->m_id;
	return true;
}

void Login::W2LUpdateLoginServerInfo()
{
	//Debug::Log("==>> Login::W2LUpdateLoginServerInfo");
	int id = m_WorldServerInfo.m_id;
	int state = (int)m_WorldServerInfo.m_State;
	string loginIp = "";
	int loginPort = 0;

	if(mp_LoginGameServer != nullptr)
	{
		loginIp = mp_LoginGameServer->m_ip;
		loginPort = mp_LoginGameServer->m_port;
	}

	Message Msg(Game::GetInstance().GetSockClient().GetFd(), eCmd::W2L_Login_Info_Update);
	Msg.AddInt(id);
	Msg.AddInt(state);
	Msg.AddString(loginIp);
	Msg.AddInt(loginPort);
	Message::Send(&Game::GetInstance().GetSockClient().GetMsgQunue(), &Msg);
}
